﻿using System;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz.QuanTriHeThong;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;

namespace QuanLyThuPhiTaiLieu.Forms.QuanTriHeThong
{
    public partial class NewNguoiSuDung : Form
    {
        private int id;

        private string userPassword;

        public NewNguoiSuDung()
        {
            InitializeComponent();
        }

        public NewNguoiSuDung(int id)
        {
            InitializeComponent();

            this.id = id;
        }

        private void SaveItem(object sender, EventArgs e)
        {
            if (Validate())
            {
                string message;

                var biz = new NguoiSuDungBiz();

                var item = new NguoiSuDung()
                {
                    Account = textAccount.Text.Trim(),
                    HoVaTen = textHoVaTen.Text.Trim(),
                    DonVi = textDonVi.Text.Trim(),                    
                    QuyenTruyCap = Convert.ToString(rdlRoles.SelectedItem)
                };

                if (id > 0)
                {
                    item.Id = id;
                    item.Password = userPassword;
                    biz.UpdateItem(item);

                    message = Constants.Messages.UpdateCompleted;
                }
                else
                {
                    item.Password = Utilities.EncodePassword(Constants.DefaultPassword);
                    biz.SaveItem(item);
                    message = Constants.Messages.InsertCompleted;
                }

                MessageBox.Show(message);
                Utilities.CloseForm(this, new ViewNguoiSuDung());
            }
        }

        private void Cancel(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new ViewNguoiSuDung());
        }

        private new bool Validate()
        {
            bool hasError = true;
            bool isFocused = false;
            errorProvider.Clear();

            if (string.IsNullOrEmpty(textAccount.Text))
            {
                errorProvider.SetError(textAccount, Constants.Messages.RequireMessage);
                hasError = false;

                textAccount.Focus();
                isFocused = true;
            }

            if (string.IsNullOrEmpty(textHoVaTen.Text))
            {
                errorProvider.SetError(textHoVaTen, Constants.Messages.RequireMessage);
                hasError = false;
                
                if (!isFocused)
                {
                    textHoVaTen.Focus();
                    isFocused = true;
                }
            }

            if (string.IsNullOrEmpty(Convert.ToString(rdlRoles.SelectedItem)))
            {
                errorProvider.SetError(rdlRoles, Constants.Messages.RequireMessage);
                hasError = false;

                if (!isFocused)
                {
                    rdlRoles.Focus();
                }
            }

            return hasError;
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                //Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.QuanTri);
                //parent.Status.Text = string.Empty;
            }

            rdlRoles.Items.Clear();
            rdlRoles.Items.Add(Constants.Securities.Guest);
            rdlRoles.Items.Add(Constants.Securities.Contributor);
            rdlRoles.Items.Add(Constants.Securities.Administrator);

            if (id > 0)
            {
                var biz = new NguoiSuDungBiz();
                var item = biz.LoadItem(id);

                textAccount.Text = Convert.ToString(item.Account);
                textHoVaTen.Text = Convert.ToString(item.HoVaTen);
                textDonVi.Text = Convert.ToString(item.DonVi);
                userPassword = item.Password;

                var role = Convert.ToString(item.QuyenTruyCap);
                var index = rdlRoles.FindString(role);
                if (index > -1)
                {
                    rdlRoles.SelectedIndex = index;
                }

                buttonReset.Visible = true;

                textAccount.ReadOnly = true;
                textAccount.TabStop = false;
                textHoVaTen.Focus();
            }
            else
            {
                textAccount.Focus();
            }            
        }

        private void ResetPassword(object sender, EventArgs e)
        {
            var biz = new NguoiSuDungBiz();
            biz.ResetPassword(id);
        }
    }
}
